package leetcode_bin_tree_test;

import bin_tree.TreeNode;

import java.util.HashMap;
import java.util.Map;

/**
 * 965. 单值二叉树
 */
public class IsSingleTree {
    static Map<Integer, Integer> map = new HashMap<>();
    public static boolean isUnivalTree(TreeNode root) {
        // 如果二叉树每个节点都具有相同的值，那么该二叉树就是单值二叉树。
        if (root == null || (root.left == null && root.right == null)) {
            return true;
        }
        preOrder(root);
        return !map.values().contains(1);
    }

    public static void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        map.put(root.val, map.getOrDefault(root.val, 0) + 1);
        preOrder(root.left);
        preOrder(root.right);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(0);
//        TreeNode node1 = new TreeNode(2);
//        TreeNode node2 = new TreeNode(2);
//        TreeNode node3 = new TreeNode(2);
//        TreeNode node4 = new TreeNode(5);
//        TreeNode node5 = new TreeNode(2);
//        TreeNode node6 = new TreeNode(2);
//        root.left = node1;
//        root.right = node2;
//        node1.left = node3;
//        node1.right = node4;
//        node2.left = node5;
//        node2.right = node6;
        System.out.println(isUnivalTree(root));
    }
}
